package com.kingyun.gpsinspection.purificationservice.services.iam.innerservice.qhse.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.iam.qhse.ContingencyPlanMapper;
import com.kingyun.gpsinspection.purificationservice.facade.model.BootstrapSearchBean;
import com.kingyun.gpsinspection.purificationservice.facade.model.iam.qhse.ContingencyPlan;
import com.kingyun.gpsinspection.purificationservice.facade.service.iam.IamFileService;
import com.kingyun.gpsinspection.purificationservice.facade.service.iam.qhse.ContingencyPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

/**
 * 应急预案服务具体实现
 */

public class ContingencyPlanServiceImpl implements ContingencyPlanService{
    @Autowired
    private IamFileService iamFileService;
   @Autowired
    private ContingencyPlanMapper contingencyPlanMapper;

    /**
     * 根据ids删除应急预案
     * @param contingencyPlanIds
     * @return
     */
    @Override
    public int deleteByPrimaryKey(String[] contingencyPlanIds){
        if(contingencyPlanIds.length>0){
           return contingencyPlanMapper.deleteByPrimaryKey(contingencyPlanIds);
        }
        return 0;
    }

    /**
     * 新增应急预案
     * @param record 应急预案
     * @param uploadFiles 需要上传的文件
     * @param deletedFiles 需要删除的文件
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int insertSelective(ContingencyPlan record,String uploadFiles ,String deletedFiles){
        // 插入数据成功之后，返回的条数
        int resultCount = 0;
        // 判断数据是否为空
        if(null == record){
            return resultCount;
        }
        // 保存业务数据
        resultCount += contingencyPlanMapper.insertSelective(record);
        // 保存附件
        resultCount += iamFileService.newInsertFile(uploadFiles,deletedFiles,record.getContingencyPlanId());

        return resultCount;
    }

    /**
     * 根据id查询
     * @param contingencyPlanId
     * @return
     */
    @Override
    public ContingencyPlan selectByPrimaryKey(String contingencyPlanId){
        if (!"".equals(contingencyPlanId)){
            return contingencyPlanMapper.selectByPrimaryKey(contingencyPlanId);
        }
        return null;
    }

    /**
     * 根据id更新应急预案
     * @param record 应急预案
     * @param uploadFiles 需要上传的文件
     * @param deletedFiles 需要删除的文件
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int updateByPrimaryKeySelective(ContingencyPlan record,String uploadFiles ,String deletedFiles){
        // 更新数据成功之后，返回的条数
        int resultCount = 0;
        // 更新数据
        if(null == record){
            return resultCount;
        }
        resultCount += contingencyPlanMapper.updateByPrimaryKeySelective(record);
        // 附件更新
        resultCount += iamFileService.newInsertFile(uploadFiles,deletedFiles,record.getContingencyPlanId());

        return resultCount;
    }

    /**
     * 查询应急预案列表
     * @param bootstrapSearchBean
     * @return
     */
    @Override
    public JSONObject contingencyPlanList(BootstrapSearchBean bootstrapSearchBean){
        // 返回结果
        JSONObject jsonObject = new JSONObject();
        // 分页设置
        bootstrapSearchBean.setLimit( bootstrapSearchBean.getLimit() == null ? Integer.valueOf(10) : bootstrapSearchBean.getLimit() );
        bootstrapSearchBean.setOffset( bootstrapSearchBean.getOffset() == null ? Integer.valueOf(0) : bootstrapSearchBean.getOffset() );
        // 总条数
        jsonObject.put("total",contingencyPlanMapper.queryCount(bootstrapSearchBean));
        jsonObject.put("rows", JSON.toJSON(contingencyPlanMapper.queryList(bootstrapSearchBean)));
        return jsonObject;
    }

    /**
     * 判断应急预案名是否存在
     * @param planName 应急预案名
     * @param orgId 组织id
     * @return true：存在 false:不存在
     */
    @Override
    public boolean isExitByName(String planName,String orgId){
        Map<String,Object> searhMap = new HashMap<>();
        searhMap.put("planName",planName);
        searhMap.put("orgId",orgId);
        return contingencyPlanMapper.isExitByName(searhMap) > 0 ? true : false;
    }
}
